<?php

/**
 * Created by PhpStorm
 * User Niu Yong Qiang
 * Date 2021/9/17 19:20
 */

namespace App\Services\Async;

use App\Enum\enum_wechat;
use App\Http\Format\FormatBase;
use App\Jobs\Oss;
use App\Models\BreederUserMessageModel;
use App\Models\ConfigModel;
use App\Models\DahuaTokenModel;
use App\Models\DahuaVideoModel;
use App\Models\OrderModel;
use App\Models\OrdproModel;
use App\Models\PetsCategoryModel;
use App\Models\PetsFoodLogsModel;
use App\Models\PetsModel;
use App\Models\TokenModel;
use App\Models\UserModel;
use App\Models\UserPublicModel;
use App\Models\UserPublicSearchModel;
use App\Models\WarehouseUseLogsModel;
use App\Services\Api\OrderService;
use App\Services\Api\UserService;
use App\Services\CommonService;
use App\Services\CurlBasiceService;
use App\Tools\AliyunSmsTool;
use App\Tools\AliyunUploadTool;
use Exception;
use Illuminate\Support\Facades\DB;
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;

class PetsService extends CommonService
{

    /**
     * 每小时运行一次-获取微信的access_token
     */
    public static function getWechatToken()
    {
        DB::beginTransaction();
        try {
            $wxxcx_config = config('wechat.wxxcx');

            //拼接参数
            $realUrl = enum_wechat::USER_ACCESS_TOKEN;
            $realUrl = str_replace("APPID", $wxxcx_config['app_id'] ?? '', $realUrl);
            $realUrl = str_replace("SECRET", $wxxcx_config['app_secret'] ?? '', $realUrl);

            $result = CurlBasiceService::getInstance()->httpGet($realUrl);
            $result = json_decode($result, true);

            if ($result['access_token'] && $result['expires_in']) {
                //入数据库
                TokenModel::insertSingle([
                    'token' => $result['access_token'],
                    'expires_in' => $result['expires_in'],
                    'created_at' => time(),
                ]);

                //写日志
                writeLoToFile('getWechatToken', 'success：', json_encode([
                    'token' => $result['access_token'],
                    'expires_in' => $result['expires_in'],
                    'created_at' => time(),
                ]));


            } else {
                //写错误日志
                writeLoToFile('getWechatToken', '微信请求error：', json_encode($result));

            }

            DB::commit();

        } catch (Exception $e) {
            DB::rollBack();
            writeLoToFile('getWechatToken', '报错error：', json_encode($e));

        }

        echo '完成';
    }

    public static function closeOrder()
    {
        $close_order = OrderModel::getUserData(['status'=> 0, ['created_at', '<', (time()-1800) ]]);
        foreach ($close_order as $value){
            OrderModel::updateSingle(['id'=> $value['id']], [
                'status'=> 5,
                'out_time'=> time(),
            ]);
        }
        echo '完成';
    }

    public static function publishTask()
    {
        $task = UserPublicModel::getUserData([['plain_end_time', '<=', time()]], 'status', [1,6]);
        foreach ($task as $value){
            UserPublicModel::updateSingle(['id'=> $value['id']], [
                'status'=> 5,
                'done_time'=> time(),
            ]);
        }

        unset($task);
        $task = UserPublicSearchModel::getUserData([['plain_end_time', '>=', time()]], 'status', [1]);
        foreach ($task as $value){
            UserPublicSearchModel::updateSingle(['id'=> $value['id']], [
                'status'=> 5,
                'done_time'=> time(),
            ]);
        }
        echo '完成';
    }
}
